<?php
/**
 * 网站博客前端
 */
class IndexController extends Controller{
	//个人博客首页
	public function index(){
		$mod = new Model("blog");
		$this->assign("hot",$mod->limit(4)->order("clicks desc")->select());
		$this->assign("jishuhot",$mod->where(" kid=2 ")->limit(4)->order("clicks desc")->select());
		$this->assign("lifehot",$mod->where(" kid=4 ")->limit(4)->order("clicks desc")->select());
		$this->assign("jishu",$mod->where(" kid=2 ")->limit(7)->select());
		$this->assign("life",$mod->where(" kid=4 ")->limit(7)->select());
		$this->assign("new",$mod->limit(4)->order("addtime desc")->select());
		$this->display("index.html");
	}
	//博客信息列表页
	public function blist(){
		$mod = new Model("blog");
		$mod2 = new Model("kind");
		$mod1 = new Model("tag");

		$this->assign("new",$mod->limit(4)->order("addtime desc")->select());
		$this->assign("hot",$mod->limit(4)->order("clicks desc")->select());
		$this->assign("jishuhot",$mod->where(" kid=2 ")->limit(4)->order("clicks desc")->select());
		$this->assign("lifehot",$mod->where(" kid=4 ")->limit(4)->order("clicks desc")->select());

		$k = !empty($_GET['k'])?$_GET['k']:2;
		$km = $mod2->find($k);	
		if (!empty($k)) {
			$mod->where(" kid=".$k);
		}
		$count = $mod->count();
		$p = new Page($count,6);		
		$this->assign("list",$mod->limit($p->limit())->order("num asc")->select());		
		$this->assign("show",$p->show());
		$this->assign("kname",$km['name']);
		$this->assign("tag",$mod1->where("state=1")->select());
		$this->display("list.html");
	}
	public function tlist(){
		$mod = new Model("blog");
		$mod1 = new Model("tag");
		$this->assign("new",$mod->limit(4)->order("addtime desc")->select());
		$this->assign("hot",$mod->limit(4)->order("clicks desc")->select());
		$this->assign("jishuhot",$mod->where(" kid=2 ")->limit(4)->order("clicks desc")->select());
		$this->assign("lifehot",$mod->where(" kid=4 ")->limit(4)->order("clicks desc")->select());

		$t = !empty($_GET['t'])?$_GET['t']:1;
		$k = $mod1->find($t);
		if (!empty($t)) {
			$mod->where(" tid=".$t);
		}
		$count = $mod->count();
		$p = new Page($count,6);
		
		$this->assign("list",$mod->limit($p->limit())->order("num asc")->select());
		$this->assign("kname",$k['name']);
		$this->assign("show",$p->show());
		$this->assign("tag",$mod1->where("state=1")->select());
		$this->display("list.html");
	}
	//博客信息详情页
	public function content(){
		$mod = new Model("blog");
		//侧边栏
		$mod1 = new Model("tag");
		$mod2 = new Model("kind");		
		$mod3 = new Model("comment");

		
		$this->assign("new",$mod->limit(4)->order("addtime desc")->select());
		$this->assign("hot",$mod->limit(4)->order("clicks desc")->select());
		$this->assign("jishuhot",$mod->where(" kid=2 ")->limit(4)->order("clicks desc")->select());
		$this->assign("lifehot",$mod->where(" kid=4 ")->limit(4)->order("clicks desc")->select());

		$id = !empty($_GET['id'])?$_GET['id']:1;
		$con = $mod->find($id);
		$this->assign("con",$con);
		//下一篇
		$n = $con['num']+1;
		$next = $mod->where("num=".$n)->select();
		$next = !empty($next)?$next[0]:0;
		$this->assign("next",$next);
		//上一篇
		$m = $con['num']-1;
		$pre = $mod->where("num=".$m)->select();
		$pre = !empty($pre)?$pre[0]:0;
		$this->assign("pre",$pre);
		//评论
		$this->assign("comment",$mod3->where("aid=".$id)->order("addtime desc")->select());

		$this->assign("tag",$mod1->where("state=1")->select());
		$this->display("content.html");
	}
	//评论
	public function comment(){
		$mod = new Model("comment");
		$_POST['addtime'] = time();
		$m = $mod->insert();
		$_POST['time'] = date('Y-m-d',time());
		if($m>0){
			echo json_encode($_POST);
		}else{
			echo 0;
		}
	}
	//遍历回复评论
	public function replyShow(){
		$mod = new Model("replycom");
		$m = array();
		$m[] = $mod->where("pid=".$_POST['pid'])->select();
		echo json_encode($m);
	}
	//ajax回复评论
	public function replyCom(){
		$mod = new Model("replycom");
		$_POST['addtime'] = time();
		$m = $mod->insert();
		if ($m>0) {
			echo json_encode($_POST);
		}else{
			echo 0;
		}
	}

}


